<!DOCTYPE html>
<html>
  <head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    
    <title>django部署小记 | Yorun Blog</title>
    
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    

    
      <link rel="icon" href="/favicon.ico">
    

    <link rel="stylesheet" href="/css/iconfont/iconfont.css">

    <link rel="stylesheet" href="/css/style.css">

    <link rel="stylesheet" href="/js/google-code-prettify/atelier-dune-light.min.css">

  </head>

  <body>
</body></html>
<header>
	<a id="logo" href="/" title="Yorun Blog">
	<img src="/favicon.ico" alt="Yorun Blog"></a>
	
	
		<!--搜索栏-->
		<i class="js-toggle-search iconfont icon-search"></i>


<form class="js-search search-form search-form--modal" method="get" role="search">
	<div class="search-form__inner">
		<div>
			<i class="iconfont icon-search"></i>
			 <input class="text-input" placeholder="Enter Key..." type="search"> 
		</div>
	</div>
</form>
	

	
		<!--侧边导航栏-->
		<a id="nav-toggle" href="#"><span></span></a>

<nav>
	<div class="menu-top-container">
		<ul id="menu-top" class="menu">
			
				
				<li class="current-menu-item">
					<a href="/" target="_self">主页</a>
				</li>
			
				
				<li class="current-menu-item">
					<a href="" target="_self">书签</a>
				</li>
			
		</ul>
	</div>
</nav>
	

</header>
<div class="m-header ">
	<section id="hero1" class="hero">
		<div class="inner">
		</div>
	</section>
	
		<figure class="top-image" style='background-image: url(/images/django-logo.png)' data-enable=false></figure>
	
</div>


<!--文章列表-->
<div class="wrapper">
  
    <!--文章-->
<article>
	
  
    <h1 class="post-title" itemprop="name">
      django部署小记
    </h1>
  

	<div class='post-body mb'>
		<p>这段时间用django开发了我的第一个网站，正好可以拿来学习一下如何部署，折腾了几天，大致走通了，赶忙记录下来。</p>
<h4 id="1-准备"><a href="#1-准备" class="headerlink" title="1.准备"></a>1.准备</h4><p>有一台可以通过外网访问的服务器，我这里使用的是阿里云的学生机，9块9一个月，非常实惠。</p>
<h4 id="2-创建一个拥有超级权限的新用户"><a href="#2-创建一个拥有超级权限的新用户" class="headerlink" title="2.创建一个拥有超级权限的新用户"></a>2.创建一个拥有超级权限的新用户</h4><pre><code># 创建一个新用户
root@localhost:~# useradd -m -s /bin/bash yorun
# 加入超级权限组
root@localhost:~# usermod -a -G sudo yorun
# 设置密码
root@localhost:~# passwd yorun
# 切换到新用户
root@localhost:~# su - yorun
# 切换成功，@符号前面已经是新用户名而不是 root 了
yorun@localhost:~$</code></pre><h4 id="3-更新软件源"><a href="#3-更新软件源" class="headerlink" title="3.更新软件源"></a>3.更新软件源</h4><pre><code># 以下都是在控制台下执行，后文省去控制台前缀
yorun@localhost:~$ sudo apt update &amp;&amp; sudo apt dist-upgrade -y</code></pre><h4 id="4-搭建Python3环境"><a href="#4-搭建Python3环境" class="headerlink" title="4.搭建Python3环境"></a>4.搭建Python3环境</h4><pre><code># 安装相关依赖
sudo apt install python3-pip
sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

# 配置.baserc文件
sudo vi ~/.baserc
# 输入以下内容
if [ -f /usr/local/bin/virtualenvwrapper.sh ];then
    export WORKON_HOME=~/.virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    source /usr/local/bin/virtualenvwrapper.sh
fi

# 切换到Python虚拟环境
mkvirtualenv new_project</code></pre><h4 id="5-从GitHub下载项目以及安装项目所需依赖"><a href="#5-从GitHub下载项目以及安装项目所需依赖" class="headerlink" title="5.从GitHub下载项目以及安装项目所需依赖"></a>5.从GitHub下载项目以及安装项目所需依赖</h4><pre><code>git clone 仓库地址
pip install -r requirements.txt</code></pre><h4 id="6-配置nginx服务器"><a href="#6-配置nginx服务器" class="headerlink" title="6.配置nginx服务器"></a>6.配置nginx服务器</h4><pre><code># 修改Nginx的配置文件
vi /etc/nginx/sites-available/default
# 替换至以下内容
listen 80;
server_name 127.0.0.1;
location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    # try_files $uri $uri/ =404;
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
    alias /home/yorun/sites/demo.xiaohuo.com/xiaohuo/static;
}</code></pre><h4 id="7-安装MySQL"><a href="#7-安装MySQL" class="headerlink" title="7.安装MySQL"></a>7.安装MySQL</h4><pre><code>sudo apt-get install mysql-server mysql-common mysql-client
sudo apt-get install libmysql-dev
sudo apt-get install libmysqlclient-dev
sudo apt-get install python-dev
pip install mysqlclient

# 导入初始数据
mysql -u root -p name &lt; dump.txt</code></pre><h4 id="8-启动服务"><a href="#8-启动服务" class="headerlink" title="8.启动服务"></a>8.启动服务</h4><pre><code># 启动gunicorn动态服务器
gunicorn --worker-class=gevent xiaohuo.wsgi:application
# 启动Nginx静态服务器
sudo service nginx restart</code></pre><h3 id="·"><a href="#·" class="headerlink" title="·"></a>·</h3><p>部署流程基本完成，小结下：<br>架构上Nginx做为静态服务器，是真正的http服务器，用于处理静态文件资源；<br>gunicorn做为动态服务器，位于http服务器和应用层之间，用于处理后台数据。<br>Nginx反向代理gunicorn，gunicorn与应用间使用wsgi通信。<br><img src="/images/web-deploy.png" alt="django-deploy"></p>

	</div>
	<div class="meta split" style="text-align:right;">
		
		<span>本文总阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>
		
		<time class="post-date" datetime="2017-08-26T14:24:50.000Z" itemprop="datePublished">2017-08-26</time>
	</div>
</article>

<!--评论-->

	<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTgxOS82Mzg1">
    <script type="text/javascript">
        (function (d, s) {
            var j, e = d.getElementsByTagName(s)[0];

            if (typeof LivereTower === 'function') { return; }

            j = d.createElement(s);
            j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
            j.async = true;

            e.parentNode.insertBefore(j, e);
        })(document, 'script');
    </script>
    <noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
</div>


<!-- 返回头 -->
<div id="back-to-top" class="back-to-top" style="display: none;">
	<a href="javascript:void(0)"><i class="iconfont icon-icon_up"></i></a>
</div>
  
</div>


  <svg id="bigTriangleColor" width="100%" height="40" viewBox="0 0 100 102" preserveAspectRatio="none">
    <path d="M0 0 L50 100 L100 0 Z"></path>
  </svg>

  

  <div class="wrapper"></div>







<div class="fat-footer">
	<div class="wrapper">
		<div class="layout layout--center">
			<div class="layout__item palm-mb">
				<div class="media">
					<img class="headimg" src='/images/profile-photo-cat.jpg' alt='Yorun'>
					<div class="media__body">
						<h4>Yorun</h4>
						<p class='site-description'>怕什么真理无穷,进一寸有一寸的欢喜</p>
					</div>
				</div>
				<div class="author-contact">
					<ul>
						
							
							<li>
				        		<a href="https://github.com/yorunw" target="_blank">
				        			
				        				<i class="iconfont icon-github"></i>
				        			
				        		</a>
				        	</li>
						
							
							<li>
				        		<a href="mailto:yorunw@gmail.com" target="_blank">
				        			
				        				<i class="iconfont icon-mail"></i>
				        			
				        		</a>
				        	</li>
						
							
							<li>
				        		<a href="https://www.jianshu.com/u/2fe0c0c3a7bf" target="_blank">
				        			
				        				<i class="iconfont icon-jianshu"></i>
				        			
				        		</a>
				        	</li>
						
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>
<footer class="footer" role="contentinfo">
	<!--<div class="wrapper wrapper--wide split split--responsive">

	</div>-->
	<div class="wrapper wrapper--wide split split--responsive">
		<span style="float:left;">© 2017-2019 by Yorun.</span>

		
		<!-- <span>本站总访问量 <span id="busuanzi_value_site_pv"></span> 次, 访客数 <span id="busuanzi_value_site_uv"></span> 人次</span> -->
		
		<span>Theme by <a href="https://github.com/justpsvm/hexo-theme-varaint">varaint</a>. 
			<!--Powered by <a href="http://hexo.io">Hexo</a>--> 
		</span>
	</div>
</footer>

	<script src="/js/lib.js"></script>
	<script src="/js/module.js"></script>
	<script src="/js/script.js"></script>
	<script src="/js/google-code-prettify/prettify.js"></script>

	
		<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	
	</body>
</html>